import { loadJSON, loadImage } from '../loaders.js';
import SpriteSheet from '../SpriteSheet.js';
import { createAnim } from '../anim.js';

export function loadSpriteSheet(name) {
  return loadJSON(`/sprites/${name}.json`)
    .then(sheetSpec => Promise.all([
      sheetSpec,
      loadImage(sheetSpec.imageURL)
    ]))
    .then(([sheetSpec, image]) => {
      const sprites = new SpriteSheet(
        image,
        sheetSpec.tileW,
        sheetSpec.tileH
      );

      if (sheetSpec.dynamic) {
        const [width, height] = sheetSpec.size;
        for (let i = 0; i < width; i++) {
          for (let j = 0; j < height; j++) {
            sprites.defineTile(`${i}x${j}`, i, j);
          }
        }
      }

      if (sheetSpec.tiles) {
        sheetSpec.tiles.forEach(tileSpec => {
          sprites.defineTile(tileSpec.name, tileSpec.index[0], tileSpec.index[1]);
        });
      }

      if (sheetSpec.frames) {
        sheetSpec.frames.forEach(frameSpec => {
          sprites.define(frameSpec.name, ...frameSpec.rect);
        });
      }

      if (sheetSpec.animations) {
        sheetSpec.animations.forEach(animSpec => {
          const animation = createAnim(animSpec.frames, animSpec.frameLen);
          sprites.defineAnim(animSpec.name, animation);
        });
      }
      return sprites;
    });
}

